package com.android.questions.ui.activity;

import android.content.Intent;
import android.content.IntentFilter;
import android.view.MotionEvent;

import androidx.viewbinding.ViewBinding;

import com.android.base.ui.base.VBBaseActivity;
import com.android.base.ui.dialog.DialogCustom;
import com.android.base.util.DQLog;
import com.android.base.util.JsonUtil;
import com.android.questions.R;
import com.android.questions.config.AnswerDataConfig;
import com.android.questions.config.AppConfig;
import com.android.questions.config.AppDataSourse;
import com.android.questions.databinding.ActivityMainBinding;
import com.android.questions.entity.AnswerEntity;
import com.android.questions.litepal.AnswerLitePalEntity;
import com.android.questions.receiver.TimeTickReceiver;
import com.android.questions.ui.fragment.HomeRunFragment;
import com.android.questions.ui.fragment.MeFragment;
import com.android.questions.util.Command;
import com.android.questions.util.DESManager;
import com.android.questions.util.sound.GameMusicManager;
import com.android.questions.util.sound.MediaBackManager;
import com.dq.ad.AdManager;

import org.litepal.LitePal;

import java.util.HashMap;
import java.util.List;

public class MainActivity extends VBBaseActivity {

    private HomeRunFragment homeFragment;
    private MeFragment meFragment;
    private TimeTickReceiver timeTickReceiver;

    @Override
    public ViewBinding getViewBinding() {
        return ActivityMainBinding.inflate(getLayoutInflater());
    }

    @Override
    public void initView() {
        super.initView();
        registerReceiver();
        if (homeFragment == null) {
            homeFragment = HomeRunFragment.createFragment();
        }
        loadRootFragment(R.id.main_fragment, homeFragment);
    }

    public void showHome() {
        pop();
    }

    public void showMe() {
        if (meFragment == null) {
            meFragment = MeFragment.createFragment();
        }
        start(meFragment);
    }

    private void registerReceiver() {
        DQLog.e("注册TIME_TICK广播");
        if (timeTickReceiver != null) {
            DQLog.e("timeTickReceiver != null");
            return;
        }
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_TIME_TICK);
        timeTickReceiver = new TimeTickReceiver();
        registerReceiver(timeTickReceiver, filter);
    }

    private void unregisterReceiver() {
        if (timeTickReceiver == null) {
            return;
        }
        unregisterReceiver(timeTickReceiver);
        timeTickReceiver = null;
    }

    @Override
    public void initData() {
        super.initData();
        AdManager.getInstance().preloadVideoAd(this);
//        JLUtils.eventCallBack("asdf", "asd");
        new Thread(new Runnable() {
            @Override
            public void run() {
                List<AnswerLitePalEntity> litePalEntities = LitePal.findAll(AnswerLitePalEntity.class);
                for (int index = 0; index < litePalEntities.size(); index++) {
                    AnswerLitePalEntity litePalEntity = litePalEntities.get(index);
                    DQLog.e("开始读取本地数据:" + litePalEntity.getTypeName());
                    String readData = AnswerDataConfig.readFileContent(MainActivity.this, litePalEntity.getAssetsName(),
                            AnswerDataConfig.dirPath + litePalEntity.getFileName());
                    DQLog.e("读取到的本地文件：" + readData.length());
                    String decryptData = DESManager.decrypt(Command.DQ_ANSWER_TEST_DATA, readData);
                    AnswerEntity entities = (AnswerEntity) JsonUtil.fromJson(decryptData, new AnswerEntity());
                    if (AppDataSourse.answerEntityList == null) {
                        AppDataSourse.answerEntityList = new HashMap<>();
                    }
                    AppDataSourse.answerEntityList.put(litePalEntity.getAnswerType(), entities.getNewslist());
                    DQLog.e("题目原数据读取结束：" + AppDataSourse.answerEntityList.size());
                }
            }
        }).start();
    }

    @Override
    public void initLister() {
        super.initLister();
    }

    @Override
    public void onBackPressedSupport() {
        new DialogCustom(this)
                .setMsg("确认退出吗？")
                .setActionLister(new DialogCustom.ActionLister() {
                    @Override
                    public void onLeftClick() {
                    }

                    @Override
                    public void onRightClick() {
                        MainActivity.this.finish();
                    }
                }).show();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (AppConfig.getBackMusic()) {
            MediaBackManager.getInstance(this).start();
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (this.onTouchEvent != null) {
            this.onTouchEvent.touchEvent(ev);
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver();
        MediaBackManager.getInstance(this).release();
        GameMusicManager.getInstance(this).release();
    }

    private OnTouchEvent onTouchEvent;

    public void setOnTouchEvent(OnTouchEvent touchEvent) {
        this.onTouchEvent = touchEvent;
    }

    public interface OnTouchEvent {
        void touchEvent(MotionEvent event);
    }
}